package com.fancy.config;

import com.lly835.bestpay.config.WxPayH5Config;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.Data;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 微信公众号配置
 * Created by FancyFong on 8/9/2017.
 */
@Data
@Component
@ConfigurationProperties(prefix = "wechat.mp")
public class WechatMpConfig {

    /**
     * 设置微信公众号的appid
     */
    private String appId;

    /**
     * 设置微信公众号的app secret
     */
    private String secret;

    /**
     * 设置微信公众号的授权回调userInfoUrl
     */
    private String userInfoUrl;

    /**
     * 微信公众平台商户号
     */
    private String mchId;

    /**
     * 微信公众平台商户秘钥
     */
    private String mchKey;

    /**
     * 微信公众平台支付异步回调
     */
    private String notifyUrl;



    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this,
                ToStringStyle.MULTI_LINE_STYLE);
    }


    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(appId);
        wxMpConfigStorage.setSecret(secret);
        return wxMpConfigStorage;
    }

    @Bean
    public BestPayServiceImpl bestPayService(){
        BestPayServiceImpl bestPayService = new BestPayServiceImpl();
        bestPayService.setWxPayH5Config(wxPayH5Config());
        return bestPayService;
    }


    @Bean
    public WxPayH5Config wxPayH5Config(){
        WxPayH5Config wxPayH5Config = new WxPayH5Config();
        wxPayH5Config.setAppId(appId);
        wxPayH5Config.setAppSecret(secret);
        wxPayH5Config.setMchId(mchId);
        wxPayH5Config.setMchKey(mchKey);
        wxPayH5Config.setNotifyUrl(notifyUrl);
        return wxPayH5Config;
    }


    public PayRequest setPayRequest(BestPayTypeEnum payType, String OrderId, Double amount, String OrderName, String openid){
        PayRequest request = new PayRequest();

        //支付请求参数
        request.setPayTypeEnum(payType);
        request.setOrderId(OrderId);
        request.setOrderAmount(amount);
        request.setOrderName(OrderName);
        request.setOpenid(openid);
        return request;
    }
}
